package com.learn.smart.framework.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReflectionUtil {
	
	private static final Logger log = LoggerFactory.getLogger(ReflectionUtil.class);
	
	public static Object newInstance(Class<?> cls){
		Object instance =null;
		try {
			instance = cls.newInstance();
		} catch (Exception e) {
			log.error("创建实例失败",e);
			e.printStackTrace();
		} 
		return instance;
	}
	
	public static Object invokeMentod(Object obj,Method method,Object... args){
		Object result =null;
		method.setAccessible(true);
		try {
			result = method.invoke(obj, args);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			log.error("调用方法失败",e);
			e.printStackTrace();
		}
		return result;
	}
	
	public static void setField(Object obj,Field field,Object value){
		field.setAccessible(true);
		try {
			field.set(obj, value);
		} catch (Exception e) {
			log.error("属性赋值失败",e);
			e.printStackTrace();
		}
	}

}
